package com.kitwee.serialport.decoder.panasonic;

import com.kitwee.serialport.decoder.InstructionDecoder;

/**
 * 松下FPX系统列解码器
 *
 * @author szhyde
 */
public class FPXInstructionToFloatDecoder extends InstructionDecoder {

    @Override
    public synchronized Object decode(byte[] source) {
        String plcCode;
        if (source[source.length - 1] == 0x0d) {
            byte[] temp = new byte[source.length - 1];
            System.arraycopy(source, 0, temp, 0, temp.length);
            plcCode = new String(temp);
        } else {
            plcCode = new String(source);
        }
        char[] sourceChar = plcCode.toCharArray();
        char[] temp = new char[sourceChar.length - 8];

        int tempIndex = 0;
        for (int i = sourceChar.length - 3; i > 5; i = i - 2) {
            if (temp.length > (tempIndex) && temp.length > 1) {
                temp[tempIndex++] = sourceChar[i - 1];
            }
            temp[tempIndex++] = sourceChar[i];
        }
        if (temp.length >= 8) {
            return getFloatRound(byte2float(new String(temp)), 100);
        }
        return byteArray2int(asciiTransformByte(temp));
    }


}
